// pages/packageA/commentDetail/commentDetail.js
import Dialog from '@vant/weapp/dialog/dialog';
var util = require('../../../utils/util')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    id: "",
    name: "",
    picUrl: "",
    index: "",
    detail: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.info('==== options ====', options)
    this.setData({
      id: options.id || '1181002',
      name: options.name || '奢华植鞣头层皮',
      picUrl: options.picUrl || 'https://hongyueshop.oss-cn-beijing.aliyuncs.com/0b5a120230403205935.png',
      index: options.index
    })
    this.getDetail()
  },

  getDetail() {
    wx.get(`${wx.api.trendGFoodsComments}?trendsId=${this.data.id}`, res => {
      console.log('==== goodsTrends ===', res)
      if (res.code == 200) {
        this.setData({
          detail: {
            ...res.data,
            ...res.data.goodsTrend
          }
        })
      }
    }, fail => {})
  },

  agree: util.throttle(function (e) {
    let {
      isAgree,
      id,
    } = this.data.detail
    let api = wx.api.trendAgree
    if (isAgree == 1) api = wx.api.trenDdeleteAgree
    wx.post(`${api}${id}`, {}, res => {
      console.log('==== agree ===', res)
      if (isAgree == 1) {
        this.setData({
          [`detail.isAgree`]: 0,
          [`detail.star`]: this.data.detail.star - 1
        })
      } else {
        this.setData({
          [`detail.isAgree`]: 1,
          [`detail.star`]: this.data.detail.star + 1
        })
      }
      this.commentUpdate()
      wx.showToast({
        title: this.data.detail.isAgree == 1 ? '谢谢支持' : '已取消',
        icon: 'none',
        duration: 1000
      })
    }, fail => {})
  }, 3000),

  toZhao(e) {
    let urls = e.currentTarget.dataset.urls
    wx.previewImage({
      showmenu: false,
      urls: urls // 需要预览的图片http链接列表
    })
  },

  showPopup(e) {
    this.setData({
      show: true
    });
  },

  onClose() {
    this.setData({
      show: false
    });
  },

  // 复制文案
  copyText(e) {
    let that = this
    wx.setClipboardData({
      data: this.data.detail.content,
      success(ress) {
        wx.getClipboardData({
          success(res) {
            that.onClose()
            console.log(res.data) // data
          }
        })
      }
    })
  },

  delete() {
    Dialog.confirm({
      title: '温馨提示',
      message: '删除评论后，评论下所有的回复都会被删除',
      confirmButtonText: '删除评论',
      cancelButtonText: '取消'
    }).then(() => {
      let id = this.data.detail.id
      wx.post(wx.api.trendDel + id, {}, res => {
        Dialog.alert({
          title: '温馨提示',
          message: '已删除',
          confirmButtonText: '好的'
        })
        this.commentDelete()
        this.onClose()
        this.back()
      }, fail => {})
    })
  },

  goBack() {
    console.log('回退回调', this.data.detail)
  },

  back() {
    wx.navigateBack({
      delta: 1
    })
  },

  // 回复
  toComment1() {
    let checkedData = this.data.detail
    wx.navigateTo({
      url: `/pages/packageA/comment/comment?id=${checkedData.id}&nickName=${checkedData.nickName}&userId=${checkedData.userId}`
    })
    this.onClose()
  },

  toComment2(e) {
    let {
      trendsId,
      fromNickName,
      userId
    } = e.currentTarget.dataset.item
    wx.navigateTo({
      url: `/pages/packageA/comment/comment?id=${trendsId}&nickName=${fromNickName}&userId=${userId}`
    })
  },

  // 更新数据
  commentUpdate() {
    wx.setStorage({
      key: "commentUpdate",
      data: {
        index: this.data.index,
        isAgree: this.data.detail.isAgree,
        star: this.data.detail.star
      },
      success() {}
    })
  },

  // 删除数据
  commentDelete() {
    wx.setStorage({
      key: "commentDelete",
      data: {
        index: this.detail.index,
      },
      success() {}
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userInfo: app.globalData.userInfo || wx.getStorageSync('userInfo')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})